<?php

class Baiviet extends MY_Admin
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();

		// Check if the logged user is an administrator
		//$this->access_library->check_access();

		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'baiviet_model', 'baiviet');
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'baiviet');
		$this->user = $this->access->get_user();
		$this->load->helper('url'); //You should autoload this one ;)
		
	}

	// Public methods
	public function index()
	{
		$this->access->restrict('Admin.Website.Baiviet.Index');
		$this->load->library('pagination');
		$this->load->helper('url');
		$this->load->helper('text');
		$config = array();
		$config['base_url'] = base_url().'admin/website/baiviet';
		$config['total_rows'] = $this->baiviet->count_all_baiviet();
		$config['per_page'] = 100;
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu tiên';
        $config['last_link'] = 'Cuối cùng';
        $config['next_link'] = 'Tiếp theo';
        $config['prev_link'] = 'Lùi lại';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		
		$lang_working = load_language_working();
		$data['title']		=	'Quản lý bài viết';
		$data['contents'] 		= $this->baiviet->get_all_article($config['per_page'], $page);
		$data['count']				= $this->baiviet->count_all_baiviet();
		$data['links'] = $this->pagination->create_links();
		$this->_template['page']	= "website/baiviet/list";
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function add()
	{
		$this->access->restrict('Admin.Website.Baiviet.Add');
		$data['title']	=	'Thêm bài viết mới';
		$lang_working = load_language_working();
		$this->load->helper($this->config->item('wysiwyg'));
		$data['categories'] = $this->baiviet->get_all_category();	
		$this->form_validation->set_rules('title', 'lang:form_article_title', 'required|max_length[250]|xss_clean');
		$this->form_validation->set_rules('alias', 'lang:form_article_alias', 'required|max_length[250]|xss_clean|callback_check_alias');
		$this->form_validation->set_rules('category', 'lang:form_article_category', 'required|max_length[260]|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_article_description', 'max_length[200]|xss_clean');
		$this->form_validation->set_rules('content', 'lang:form_article_content', 'trim|required');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$config['upload_path'] = './uploads/content';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		$data['error'] = '';
		
		
		if ($this->form_validation->run() == TRUE)
		{
			if($_FILES['userfile']['name']=='')
			{
				$this->baiviet->add_baiviet();
				$this->session->set_flashdata('message', lang('successfully_edited'));
				redirect('admin/website/baiviet', 'refresh');
			}
			
			else 
			{
				if(!$this->upload->do_upload())
				{
					$this->session->set_flashdata('error', $this->upload->display_errors());
					redirect($this->uri->uri_string());
				}
				else
				{
					$data =  $this->upload->data();	
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_450x250',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'400',
							'height'=>'250',
							'new_image'=>$data['file_path'].'medium'
					);	
					$this->load->library('image_lib');
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
					
					// Medium
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_120x90',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'120',
							'height'=>'90',
							'new_image'=>$data['file_path'].'small'
					);	
					//var_dump($data['file_path']); exit();
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
					
					
					$data['image_thumb'] = $data['file_name'];	
					
					$this->baiviet->add_article($data);
					$this->session->set_flashdata('message', lang('successfully_edited'));
					redirect('admin/website/baiviet', 'refresh');
				}
			}
			
		}
		
		$this->_template['page']	= 'website/baiviet/add';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function check_alias()
	{
        if($this->baiviet->check_alias())
        {
            $this->form_validation->set_message('check_alias','Alias already exist please choose onther one');
            return false;
        }
        else
        {
            
            return true;
        }
    }

	public function edit($id = null)
	{
		$this->access->restrict('Admin.Website.Baiviet.Edit');
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		
		$this->load->helper($this->config->item('wysiwyg'));
		$data['error'] = '';
		$data['title']	=	'Chỉnh sửa bài viết';
		$lang_working = load_language_working();
		$data['article'] = $this->baiviet->get_article($id);
		$thumb = $data['article']->content_image;
		//echo $thumb;
		if($data['article']->alias === strtolower(auto_alias($this->input->post('alias'))) || $this->input->post('alias')=='')
		{
			$this->form_validation->set_rules('alias', 'lang:form_article_alias', 'max_length[160]|xss_clean');
		}
		else
		{
			$this->form_validation->set_rules('alias', 'lang:form_article_alias', 'max_length[160]|xss_clean|callback_check_alias');
		}
		//$data['categories'] = array_remove_item_id($this->category->get_categories($lang_working['id']),$id);	
		$this->form_validation->set_rules('title', 'lang:form_article_title', 'required|max_length[160]|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_article_description', 'max_length[200]|xss_clean');
		$this->form_validation->set_rules('content', 'lang:form_article_content', 'trim|required');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$data['categories'] = $this->baiviet->get_all_category($id);
		
		$config['upload_path'] = './uploads/content';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		if ($this->form_validation->run() == TRUE)
		{//echo $this->input->post('content', FALSE);  exit();
			//$this->input->post(NULL, TRUE);
			if($_FILES['userfile']['name']=='')
			{
				$this->baiviet->edit_article1($id);
				//echo $this->input->post('content'); exit();
				$this->session->set_flashdata('message', lang('successfully_edited'));
				redirect('admin/website/baiviet', 'refresh');
			}
			
			else 
			{
				if(!$this->upload->do_upload())
				{
					$this->session->set_flashdata('error', $this->upload->display_errors());
					redirect($this->uri->uri_string());
				}
				else
				{
					$data =  $this->upload->data();	
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_450x250',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'400',
							'height'=>'250',
							'new_image'=>$data['file_path'].'medium'
					);	
					$this->load->library('image_lib');
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
					
					// Medium
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_120x90',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'120',
							'height'=>'90',
							'new_image'=>$data['file_path'].'small'
					);	
					
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
					
					
					$data['image_thumb'] = $data['file_name'];
					
					$this->baiviet->edit_article($id,$data,$thumb);
					$this->session->set_flashdata('message', lang('successfully_edited'));
					redirect('admin/website/baiviet', 'refresh');
				}
			}
			
		}
		
		$this->_template['page']	= 'website/baiviet/edit';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}

	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->baiviet->change_status_article($id,1);
		redirect('admin/website/baiviet', 'refresh');
		
	}
	
	public function featured($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->baiviet->change_featured_article($id,1);
		redirect('admin/website/baiviet', 'refresh');
		
	}
	
	public function nofeatured($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->baiviet->change_featured_article($id,0);
		redirect('admin/website/baiviet', 'refresh');
		
	}
	
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->baiviet->change_status_article($id,0);
		redirect('admin/website/baiviet', 'refresh');
	}
	public function delete($id)
	{
		$this->access->restrict('Admin.Website.Baiviet.Delete');
		$this->baiviet->delete_article($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/website/baiviet', 'refresh');
	}
}

/* End of file categories.php */
/* Location: ./application/modules/admin/controllers/categories.php */